﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;

namespace Components.PictureBox
{
    public class PictureBox: System.Windows.Forms.PictureBox
    {
        ContextMenuStrip cms = new ContextMenuStrip();
        ToolStripButton tsbLimpar = new ToolStripButton("Limpar Imagem");
        ToolStripButton tsbAlterar = new ToolStripButton("Alterar Imagem");

        [DefaultValue(BorderStyle.FixedSingle)]
        public new BorderStyle BorderStyle
        {
            get { return base.BorderStyle; }
            set { base.BorderStyle = value; }
        }

        [DefaultValue(PictureBoxSizeMode.StretchImage)]
        public new PictureBoxSizeMode SizeMode 
        {
            get { return base.SizeMode; }
            set { base.SizeMode = value; }
        }

        public PictureBox()
        {
            this.ContextMenuStrip = cms;
            this.SizeMode = PictureBoxSizeMode.StretchImage;
            this.cms.Items.Add(tsbLimpar);
            this.cms.Items.Add(tsbAlterar);
            this.BorderStyle = BorderStyle.FixedSingle;
            this.tsbLimpar.Click += new EventHandler(tsbLimpar_Click);
            this.tsbAlterar.Click += new EventHandler(tsbAlterar_Click);
        }

        void tsbAlterar_Click(object sender, EventArgs e)
        {
            OpenFileDialog opf = new OpenFileDialog();
            if (opf.ShowDialog() == DialogResult.OK)
                AlteraImagem(opf.FileName);
        }

        public void AlteraImagem(string path)
        {
            this.Image = System.Drawing.Image.FromFile(path);
        }

        public void AlteraImagem(System.IO.Stream stream)
        {
            this.Image = System.Drawing.Image.FromStream(stream);
        }

        void tsbLimpar_Click(object sender, EventArgs e)
        {
            this.Image = null;
        }
    }
}
